package com.jiangjiang.eshop.commodity.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiangjiang.eshop.commodity.convert.PropertyGroupRelationshipConvert;
import com.jiangjiang.eshop.commodity.dao.PropertyGroupRelationshipMapper;
import com.jiangjiang.eshop.commodity.domain.PropertyGroupRelationshipEntity;
import com.jiangjiang.eshop.commodity.dto.PropertyGroupRelationshipDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @ClassName PropertyGroupRelationshipService
 * @Description TODO
 * @Author jhw
 * @Date 2021/6/20 16:26
 * @Version 1.0
 **/
@Service
public class PropertyGroupRelationshipService
        extends ServiceImpl<PropertyGroupRelationshipMapper, PropertyGroupRelationshipEntity> {

    @Autowired
    private PropertyGroupRelationshipConvert propertyGroupRelationshipConvert;

    /**
     * 批量保存属性组和属性关联关系
     * @param relationshipDTOList
     */
    public void saveBatch(List<PropertyGroupRelationshipDTO> relationshipDTOList) {
        if(CollectionUtils.isEmpty(relationshipDTOList)) {
            return;
        }
        this.saveBatch(propertyGroupRelationshipConvert.convertDTOList2Entities(relationshipDTOList));
    }

    /**
     * 根据属性分组集合删除所有属性分组和属性间的关联关系
     * @param propertyGroupIds
     */
    public void batchRemoveByPropertyGroupIds(List<Long> propertyGroupIds) {
        LambdaQueryWrapper<PropertyGroupRelationshipEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.in(PropertyGroupRelationshipEntity::getPropertyGroupId, propertyGroupIds);
        this.remove(wrapper);
    }

    public List<PropertyGroupRelationshipDTO> queryByPropertyGroupId(Long propertyGroupId){
        LambdaQueryWrapper<PropertyGroupRelationshipEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(PropertyGroupRelationshipEntity::getPropertyGroupId, propertyGroupId);
        return propertyGroupRelationshipConvert.convertEntities2DTOList(this.list(wrapper));
    }
}
